package com.tongtech.proxy.core.crypto;

import java.util.ArrayList;
import java.util.List;

public class ProfilesEncode {
    /**
     * 多少个板子
     **/
    private static final int N = 4;

    // 底部位置
    private static final int bottom = 0;

    // 顶部位置
    private static final int top = 2000;

    // 原始位置
    private static final Integer[] origPos = new Integer[]{
            (1 << 5) + (1 << 1),
            (1 << 9) + (1 << 6) + (1 << 5) + (1 << 2),
            ( (1 << 9) + (1 << 7) + (1 << 6) + (1 << 3) + (1 << 2))-(1 << 5) + (1 << 1),
            (1 << 9) + (1 << 8) + (1 << 7) + (1 << 6) + (1 << 5) + (1 << 2)
    };

    // 平均间隔位置
    private static final int interval = top / (N + 1);

    public static void main(String[] args) {
        handler(origPos, null, null);
    }


    private static void handler(Integer[] snapshot, Integer moveNumber, List<Integer> result) {
        if (snapshot == null || snapshot.length == 0) throw new RuntimeException("tree node is null!");

        List<Integer> resultArr = new ArrayList<>();

        if (moveNumber == null) {
            List<Integer> nextStep = searchForNextStep(snapshot);
            for (Integer step : nextStep) {
                handler(snapshot, step, resultArr);
            }
        } else {
            Integer[] stepPos = new Integer[snapshot.length];
            System.arraycopy(snapshot, 0, stepPos, 0, stepPos.length);
            // 移动完成
            stepPos[moveNumber] = (moveNumber + 1) * interval;

            if (result != null && result.size() > 0) {
                // 追加之前的结果
                resultArr.addAll(result);
            }
            resultArr.add(moveNumber + 1);


            List<Integer> nextStep = searchForNextStep(stepPos);
            if (nextStep.size() == 0) {

                int sum = 0;
                for (int i = 0; i < stepPos.length; i++) {
                    if (stepPos[i].equals((i + 1) * interval)) sum++;
                }

                if (sum == stepPos.length)
                    System.out.println(resultArr);
            }

            for (Integer next : nextStep) {
                handler(stepPos, next, resultArr);
            }
        }

    }


    public static List<Integer> searchForNextStep(Integer[] snapshot) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < snapshot.length; i++) {
            // 当前板层已经移动到正确位置了
            Integer correctPos = (i + 1) * interval;
            if (correctPos.equals(snapshot[i])) continue;

            // 当前位置
            Integer element = snapshot[i];
            // 正确位置

            if (correctPos > element) {
                // 正确位置 > 当前位置 向上移动
                if (i == (snapshot.length - 1)) {
                    if (top - element > correctPos - element)
                        result.add(i);
                } else if (snapshot[i + 1] - element >= correctPos - element) {
                    result.add(i);
                }
            } else {
                // 正确位置 < 当前位置 向下移动
                if (i == 0) {
                    result.add(i);
                } else if (element + snapshot[i - 1] >= element - correctPos) {
                    result.add(i);
                }

            }
        }
        return result;
    }

}
